package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

type UserController struct {
	Ctx iris.Context
}

// GetInfo 获取当前用户登录信息
// /user/info
func (c *UserController) GetInfo() mvc.Result {
	user := getCurrentUser(c.Ctx)
	roles, err := service.UserService.GetUserRolesByID(user.UserID)
	if err != nil {
		return action.ServerError()
	}
	permissions, err := service.UserService.GetMenuPermission(user.UserID)
	if err != nil {
		return action.ServerError()
	}
	return action.Data(map[string]any{
		"user": map[string]any{
			"userId":   user.UserID,
			"userName": user.UserName,
			"nickName": user.NickName,
			"avatar":   user.Avatar,
		},
		"roles":       roles,
		"permissions": permissions,
	})
}

// GetRouters 获取当前登录用户的前端菜单路由
func (c *UserController) GetRouters() mvc.Result {
	user := getCurrentUser(c.Ctx)
	menus, err := service.MenuService.GetMenuTreeByUserId(user.UserID)
	if err != nil {
		return action.ServerError()
	}
	router, err := service.MenuService.BuildMenus(menus)
	if err != nil {
		return action.ServerError()
	}
	return action.Data(router)
}
